#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File Name: MarkdownRender.py
# Author: bobo
# Created Time: 202411091200
# pip install markdown
import markdown
import mistune

# markdown库扩展
from utils.MarkDownExtension import (
    DelExtension,
    HedgeAlertExtension,
)
from utils.MistuneExtension import hedgeAlert


def render_markdown2(content: str):
    """渲染md内容为html，使用markdown库"""
    # markdown库
    exts = [
        "markdown.extensions.extra",
        "markdown.extensions.codehilite",
        "tables",
        "toc",
        "md_in_html",
        DelExtension(),
        HedgeAlertExtension(),
    ]
    html_content = markdown.markdown(content, extensions=exts)
    return html_content


def render_markdown(content: str):
    """渲染md内容为html，使用mistune库"""
    md_mistune = mistune.create_markdown(
        escape=False,
        plugins=[
            "footnotes",  # 注脚
            "strikethrough",  # 删除线
            "table",  # 表格
            "speedup",
            "url",  # 链接
            "task_lists",  # 任务列表
            "def_list",  # 自定义列表
            "abbr",  # 缩写
            "mark",
            "insert",
            "superscript",
            hedgeAlert,
        ],
    )
    html_content = md_mistune(content)
    return html_content


def render_markdown_file(md_file_path: str):
    """渲染md文件为html"""
    with open(md_file_path, "r", encoding="utf-8") as file:
        content = file.read()
    return render_markdown(content)
